package com.emm.yixun.customer.service.impl.app.manage;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.CustomerService;
import com.emm.yixun.basic.service.DataPermissionService;
import com.emm.yixun.basic.service.RoleDataAuthorityService;
import com.emm.yixun.basic.service.UserService;
import com.emm.yixun.common.app.GetRecommendCustomerListReqDto;
import com.emm.yixun.common.app.GetRecommendCustomerListResDto;
import com.emm.yixun.common.app.servletentity.RecommendCustomerListRes;
import com.emm.yixun.common.enums.ResultEnum;
import com.emm.yixun.common.model.Customer;
import com.emm.yixun.common.model.dto.CustomerDto;

@Service("getRecommendCustomerListManage")
public class GetRecommendCustomerListManage {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleDataAuthorityService roleDataAuthorityService;
	@Autowired
	private DataPermissionService dataPermissionService;
	@Autowired
	private CustomerService customerService;
	
	/**
	 * 查询客户推荐人列表
	 * @param reqDto
	 * @return
	 */
	public GetRecommendCustomerListResDto getRecommendCustomerList(GetRecommendCustomerListReqDto reqDto) {
		GetRecommendCustomerListResDto resDto = new GetRecommendCustomerListResDto();
		try{
			//获取当前用户角色权限下的客户
			CustomerDto customerDto = new CustomerDto();
			String dataPermission = dataPermissionService.dataPermissionCondition(Long.parseLong(reqDto.getUserId()),
					reqDto.getResourcesCode(), null, null);
			String[] dataPermissionArr = dataPermission.split(":");
			if ("00".equals(dataPermissionArr[0])) {
				resDto.setRecommendCustomerList(null);
			}
			customerDto.setDataPermission(dataPermissionArr[1]);
			customerDto.setProjectCode(reqDto.getProjectCode());
			List<Customer> customerList = customerService.selectListByDto(customerDto);
			if(null != customerList && customerList.size() > 0){
				List<RecommendCustomerListRes> recommendCustomerList = new ArrayList<RecommendCustomerListRes>();
				for(Customer customer: customerList){
					RecommendCustomerListRes recCusList = new RecommendCustomerListRes();
					recCusList.setCustomerId(String.valueOf(customer.getId()));
					recCusList.setCustomerName(customer.getCustomerName());
					recommendCustomerList.add(recCusList);
				}
				resDto.setRecommendCustomerList(recommendCustomerList);
			}else{
				resDto.setRecommendCustomerList(null);
			}
			resDto.setResult(ResultEnum.SUCCESS.getNo());
		}catch(Exception e){
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("查询推荐人客户失败!");
			resDto.setResult(ResultEnum.FAILURE.getNo());
			return resDto;
		}
		return resDto;
	}
}
